<template>
	<view>
		<head-navbar :back="true">
			<view>售后</view>
		</head-navbar>
		<view class="content">
			<view class="navbar">
				<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{current: tabCurrentIndex === index}"
				 @click="tabClick(index)">
					{{item.text}}
				</view>
			</view>

			<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab" :style="{height:getTabbarContentHeight}">
				<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
					<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
						<!-- 空白页 -->
						<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>

						<!-- 订单列表 -->
						<view v-for="(item,index) in tabItem.orderList" :key="index" class="order-item">
							<view class="i-top b-b">
								<text class="time"><text>订单号</text>{{item.time}}</text>
							</view>

							<view class="goods-box-single" v-for="(goodsItem, goodsIndex) in item.goodsList" :key="goodsIndex">
								<image class="goods-img" :src="goodsItem.image" mode="aspectFill"></image>
								<view class="right">
									<text class="title clamp">{{goodsItem.title}}</text>
									<view class="attr-box">
										<view class="left">
											<text>规格：</text> {{goodsItem.attr}}
										</view>
										<view class="">
											x {{goodsItem.number}}
										</view>
									</view>
									<view class="price">
										<text class="left">实付款:</text>
										<text class="rightTxt">￥{{goodsItem.price}}</text>
									</view>
								</view>
							</view>
							<view class="action-box b-t" v-if="item.state != 0">
								<button class="action-btn" @click="toggleSpec" v-if="tabCurrentIndex == 0"> 申请退款</button>
								<!-- <button class="action-btn" @click="cancelOrder(item)">取消订单</button>
								<button class="action-btn recom">立即支付</button> -->
							</view>
						</view>

						<uni-load-more :status="tabItem.loadingType"></uni-load-more>

					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	
		<!-- 规格-模态层弹窗 -->
		<view class="popup spec" :class="specClass">
			<!-- 遮罩层 -->
			<view class="mask"></view>
			<view class="layer attr-content">
				<image src="../../../../../static/img/quxiao@2x.png" mode="" class="close" @click="toggleSpec"></image>
				<view class="title">
					申请退货
				</view>
				<view class="pickInput" @click="open()">
					<view>{{param.reason}}</view>
					<view class="rightIcon iconfont icon-fanhui"></view>
				</view>
				<bankpicker column-num="1" :picker-list="piackList" @confirm="confirm" ref="bankpicker"></bankpicker> 
				<textarea placeholder-style="color:#CDCDCD" placeholder="请描述申请售后服务的具体原因…" :value="param.memo"/>
				<view class="imgbox">
					<view class="img" @click="uploadImg('onePhoto')" v-if="param.onePhoto == null  || param.onePhoto == ''">点击上传照片</view>
					<image :src="param.onePhoto" mode="" class="" v-else class="img"></image>
					<view class="img" @click="uploadImg('twoPhoto')" v-if="param.twoPhoto == null  || param.twoPhoto == ''">点击上传照片</view>
					<image :src="param.twoPhoto" mode="" class="" v-else  class="img"></image>
					<view class="img" @click="uploadImg('threePhoto')" v-if="param.threePhoto == null  || param.threePhoto == ''">点击上传照片</view>
					<image :src="param.threePhoto" mode="" class="" v-else class="img"></image>
				</view>
				<button class="btn" @click="buy">提交工单</button>
			</view>
		</view>
	</view>
</template>

<script>
	import empty from "@/components/empty";
	import bankpicker from '@/components/my-picker/Picker.vue';
	import {
		pathToBase64,
		base64ToPath
	} from '../../../../../utils/imgTool.js';
	export default {
		computed: {
			// 获取tabbar内容高度=设备可视区高度-手机头部状态栏高度-APP头部导航栏高度(44)-底部菜单高度（50）-搜索框高度（35）-tabbarTitle高度（40）-安全距离（20）
			getTabbarContentHeight() {
				return this.globalData.systemInfo.windowHeight - this.globalData.systemInfo.statusBarHeight - 44  - 40 - 20 + 'px'
			}
		},
		components: {
			empty,
			bankpicker
		},
		data() {
			return {
				specClass: 'none',
				piackList:[
					{
						label: '选项2',
						value: 1
					}
				],
				param:{
					reason:'申请原因',
					memo:'',
					onePhoto:'',
					twoPhoto:'',
					threePhoto:''
				},
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '售后申请',
						loadingType: 'more',
						orderList: []
					},
					{
						state: 1,
						text: '处理中',
						loadingType: 'more',
						orderList: []
					},
					{
						state: 2,
						text: '已完成',
						loadingType: 'more',
						orderList: []
					}
				],
				orderList: [{
						time: '2019-04-06 11:37',
						state: 1,
						goodsList: [{
							title: '古黛妃 短袖t恤女 春夏装2019新款韩版宽松',
							price: 179.5,
							image: 'https://img13.360buyimg.com/n8/jfs/t1/30343/20/1029/481370/5c449438Ecb46a15b/2b2adccb6dc742fd.jpg',
							number: 1,
							attr: '珊瑚粉 M'
						}]
					},
					{
						time: '2019-04-06 11:37',
						state: 9,
						goodsList: [{
							title: '古黛妃 短袖t恤女 春夏装2019新款韩版宽松',
							price: 179.5,
							image: 'https://img13.360buyimg.com/n8/jfs/t1/30343/20/1029/481370/5c449438Ecb46a15b/2b2adccb6dc742fd.jpg',
							number: 1,
							attr: '珊瑚粉 M'
						}]
					},
					{
						time: '2019-04-06 11:37',
						state: 1,
						goodsList: [{
							title: '古黛妃 短袖t恤女 春夏装2019新款韩版宽松',
							price: 179.5,
							image: 'https://img13.360buyimg.com/n8/jfs/t1/30343/20/1029/481370/5c449438Ecb46a15b/2b2adccb6dc742fd.jpg',
							number: 1,
							attr: '珊瑚粉 M'
						}]
					},
					{
						time: '2019-04-06 11:37',
						state: 1,
						goodsList: [{
							title: '回力女鞋高帮帆布鞋女学生韩版鞋子女2019潮鞋女鞋新款春季板鞋女',
							price: 69,
							image: 'https://img.alicdn.com/imgextra/i3/2128794607/TB2gzzoc41YBuNjy1zcXXbNcXXa_!!2128794607.jpg_430x430q90.jpg',
							number: 1,
							attr: '白色-高帮 39'
						}]
					},
					{
						time: '2019-04-06 11:37',
						state: 1,
						goodsList: [{
							title: '古黛妃 短袖t恤女 春夏装2019新款韩版宽松',
							price: 179.5,
							image: 'https://img13.360buyimg.com/n8/jfs/t1/30343/20/1029/481370/5c449438Ecb46a15b/2b2adccb6dc742fd.jpg',
							number: 1,
							attr: '珊瑚粉 M'
						}]
					},
					{
						time: '2019-04-06 11:37',
						state: 1,
						goodsList: [{
							title: '古黛妃 短袖t恤女 春夏装2019新款韩版宽松',
							price: 179.5,
							image: 'https://img13.360buyimg.com/n8/jfs/t1/30343/20/1029/481370/5c449438Ecb46a15b/2b2adccb6dc742fd.jpg',
							number: 1,
							attr: '珊瑚粉 M'
						}]
					}

				]
			}
		},
		onLoad(options) {
			this.tabCurrentIndex = +options.state;
			// #ifndef MP
			this.loadData()
			// #endif
			// #ifdef MP
			if (options.state == 0) {
				this.loadData()
			}
			// #endif
		},
		methods: {
			uploadImg(type) {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: (res) => {
						uni.getImageInfo({
							src: res.tempFiles[0].path,
							success: (res) => {
								pathToBase64(res.path)
									.then(base64 => {
										this.param[type] = base64;
									})
									.catch(error => {
										this.utils.toast('转换Base64失败');
									})
							},
							fail: (err) => {
								this.utils.toast('上传图片失败');
							}
						})
					},
					fail: (err) => {
				
					}
				})
			},
			open(){
				this.$refs.bankpicker.showPicker();
			},
			//弹窗开关
			toggleSpec() {
				if (this.specClass === 'show') {
					this.specClass = 'hide';
					setTimeout(() => {
						this.specClass = 'none';
					}, 250);
				} else if (this.specClass === 'none') {
					this.specClass = 'show';
				}
			},
			//获取订单列表
			loadData(source) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;

				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}

				navItem.loadingType = 'loading';

				setTimeout(() => {
					let orderList = this.orderList.filter(item => {
						//添加不同状态下订单的表现形式
						item = Object.assign(item, this.orderStateExp(item.state));
						//演示数据所以自己进行状态筛选
						if (state === 0) {
							//0为全部订单
							return item;
						}
						return item.state === state
					});
					orderList.forEach(item => {
						navItem.orderList.push(item);
					})
					//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
					this.$set(navItem, 'loaded', true);

					//判断是否还有数据， 有改为 more， 没有改为noMore 
					navItem.loadingType = 'more';
				}, 600);
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			orderStateExp(state) {
				let stateTip = '',
					stateTipColor = '#fa436a';
				switch (+state) {
					case 1:
						stateTip = '待付款';
						break;
					case 2:
						stateTip = '待发货';
						break;
					case 9:
						stateTip = '订单已关闭';
						stateTipColor = '#909399';
						break;

						//更多自定义
				}
				return {
					stateTip,
					stateTipColor
				};
			},
			confirm(picked) {
				console.log(picked)
				// this.toggleSpec();
				this.param.reason = picked.labels.join('')
			},
		}
	}
</script>

<style lang="scss">
	@import './index.scss';
</style>
